<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <_SerializationAssemblyName>$(AssemblyName).XmlSerializers</_SerializationAssemblyName>
    <_SerializerDllIntermediateFolder>$(IntermediateOutputPath)$(_SerializationAssemblyName).dll</_SerializerDllIntermediateFolder>
    <_SerializerPdbIntermediateFolder>$(IntermediateOutputPath)$(_SerializationAssemblyName).pdb</_SerializerPdbIntermediateFolder>
    <_SerializerCsIntermediateFolder>$(IntermediateOutputPath)$(_SerializationAssemblyName).cs</_SerializerCsIntermediateFolder>
    <_SerializerCsAssemblyInfoIntermediateFolder>$(IntermediateOutputPath)SgenAssemblyInfo.cs</_SerializerCsAssemblyInfoIntermediateFolder>
    <_SGenWarningText>SGEN: Failed to generate the serializer for $(AssemblyName)$(TargetExt). Please follow the instructions at https://go.microsoft.com/fwlink/?linkid=858594 and try again.</_SGenWarningText>
    <_SGenRspWarningText>Failed to generate response file for Microsoft.XmlSerializer.Generator, serializer is generating with default settings.</_SGenRspWarningText>
    <_SerializationAssemblyDisabledWarnings>$(NoWarn);219;162;$(SerializationAssemblyDisabledWarnings)</_SerializationAssemblyDisabledWarnings>
    <_SgenRspFilePath>$(IntermediateOutputPath)sgen.rsp</_SgenRspFilePath>
    <_CscRspFilePath>$(IntermediateOutputPath)csc.rsp</_CscRspFilePath>
  </PropertyGroup>

  <Target Name="GenerateRspFiles" BeforeTargets="GenerateSerializationAssembly">
    <PropertyGroup>
      <SGenReferences Condition="'$(SGenReferences)' == ''">@(ReferencePath)</SGenReferences>
    </PropertyGroup>
    <ItemGroup>
      <SgenRspFile Include ="--reference $(SGenReferences.Replace(';', '%3B'))" />
      <SgenRspFile Include ="--type $(SGenTypes.Replace(';', '%3B'))" Condition="'$(SGenTypes)' != ''" />
      <SgenRspFile Include ="--verbose" Condition="'$(SGenVerbose)' == 'true'" />
      <SgenRspFile Include ="--proxytypes" Condition="'$(SGenProxyTypes)' == 'true'" />
      <CscRspFile Include="-keyfile:$(SGenKeyFile)" Condition="'$(SGenKeyFile)' != ''" />
      <CscRspFile Include="-keycontainer:$(SGenKeyContainer)" Condition="'$(SGenKeyContainer)' != ''" />
      <CscRspFile Include ="-delaysign" Condition="'$(SGenDelaySign)' == 'true'" />
    </ItemGroup>
    <WriteLinesToFile File="$(_SgenRspFilePath)" Lines="@(SgenRspFile, '%0A')" Overwrite="true" ContinueOnError="true" />
    <Warning Condition="!Exists('$(_SgenRspFilePath)')" Text="$(_SGenRspWarningText)" />
    <WriteLinesToFile Condition="'@(CscRspFile)'!=''" File="$(_CscRspFilePath)" Lines="@(CscRspFile, '%0A')" Overwrite="true" ContinueOnError="true" />
  </Target>

  <Target Name="GenerateAssemblyVersionInfo" BeforeTargets="GenerateSerializationAssembly">  
    <GetAssemblyIdentity AssemblyFiles="$(IntermediateOutputPath)$(AssemblyName)$(TargetExt)" > 
      <Output TaskParameter="Assemblies" ItemName="SerializationAssemblyVersionInfo" />
    </GetAssemblyIdentity>
    <WriteLinesToFile File="$(_SerializerCsAssemblyInfoIntermediateFolder)" 
      Lines="using System.Reflection%3B

      [assembly: AssemblyVersion( &quot;%(SerializationAssemblyVersionInfo.Version)&quot; )]
      [assembly: AssemblyFileVersion( &quot;%(SerializationAssemblyVersionInfo.Version)&quot; )]"
      Overwrite="true"
      Encoding="Unicode"/>      
  </Target> 

  <Target Name="GenerateSerializationAssembly" AfterTargets="Build" Inputs="@(ReferencePath);@(IntermediateAssembly)" Outputs="$(_SerializerDllIntermediateFolder)">
    <Delete Condition="Exists('$(_SerializerDllIntermediateFolder)') == 'true'" Files="$(_SerializerDllIntermediateFolder)" ContinueOnError="true"/>
    <Delete Condition="Exists('$(_SerializerPdbIntermediateFolder)') == 'true'" Files="$(_SerializerPdbIntermediateFolder)" ContinueOnError="true"/>
    <Delete Condition="Exists('$(_SerializerCsIntermediateFolder)') == 'true'"  Files="$(_SerializerCsIntermediateFolder)" ContinueOnError="true"/>
    <Message Text="Running Serialization Tool" Importance="normal" />
    <Exec Command="dotnet Microsoft.XmlSerializer.Generator &quot;$(IntermediateOutputPath)$(AssemblyName)$(TargetExt)&quot; --force --quiet $(_SgenRspFilePath)" ContinueOnError="true"/>
    <Warning Condition="Exists('$(_SerializerCsIntermediateFolder)') != 'true'" Text="$(_SGenWarningText)" />
    <Csc Condition="Exists('$(_SerializerCsIntermediateFolder)') and !Exists('$(_CscRspFilePath)')" ContinueOnError="true" OutputAssembly="$(_SerializerDllIntermediateFolder)" References="@(ReferencePath);@(IntermediateAssembly)" Optimize="$(Optimize)" EmitDebugInformation="$(DebugSymbols)" Sources="$(_SerializerCsIntermediateFolder);$(_SerializerCsAssemblyInfoIntermediateFolder)" TargetType="Library" ToolExe="$(CscToolExe)" ToolPath="$(CscToolPath)" DisabledWarnings="$(_SerializationAssemblyDisabledWarnings)"/>
    <Csc Condition="Exists('$(_SerializerCsIntermediateFolder)') and Exists('$(_CscRspFilePath)')" ContinueOnError="true" OutputAssembly="$(_SerializerDllIntermediateFolder)" References="@(ReferencePath);@(IntermediateAssembly)" Optimize="$(Optimize)" EmitDebugInformation="$(DebugSymbols)" Sources="$(_SerializerCsIntermediateFolder);$(_SerializerCsAssemblyInfoIntermediateFolder)" TargetType="Library" ResponseFiles="$(_CscRspFilePath)"  ToolExe="$(CscToolExe)" ToolPath="$(CscToolPath)" DisabledWarnings="$(_SerializationAssemblyDisabledWarnings)"/>
    <Warning Condition="Exists('$(_SerializerDllIntermediateFolder)') != 'true' And Exists('$(_SerializerCsIntermediateFolder)') == 'true'" Text="$(_SGenWarningText)"/>
    <Copy Condition="Exists('$(_SerializerDllIntermediateFolder)') == 'true'" SourceFiles="$(_SerializerDllIntermediateFolder)" DestinationFolder="$(OutputPath)" />
    <Delete Condition="Exists('$(_SgenRspFilePath)')" Files="$(_SgenRspFilePath)" />
    <Delete Condition="Exists('$(_CscRspFilePath)')" Files="$(_CscRspFilePath)" />
  </Target>

  <Target Name="CleanSerializationAssembly" AfterTargets="CoreClean">
    <Message Text="Cleaning serialization files..." Importance="normal"/>
    <Delete Condition="Exists('$(OutputPath)\$(_SerializationAssemblyName).dll') == 'true'" Files="$(OutputPath)\$(_SerializationAssemblyName).dll" />
  </Target>

  <Target Name="CopySerializationAssembly" AfterTargets="PrepareForPublish">
    <Copy Condition="Exists('$(OutputPath)\$(AssemblyName).XmlSerializers.dll')=='true'" SourceFiles="$(OutputPath)\$(AssemblyName).XmlSerializers.dll" DestinationFolder="$(PublishDir)" SkipUnchangedFiles="false" />
  </Target>

  <Target Name="GenerateSerializationAssemblyForReferenceAssemblies" AfterTargets="GenerateSerializationAssembly" Condition="@(SerializationAssembly)!=''">
    <ItemGroup>
      <_SearchSerializationAssembly Include="@(Reference)">
        <AssemblyName>%(SerializationAssembly.Identity)</AssemblyName>
        <SerializationTypes>%(SerializationAssembly.SerializationType)</SerializationTypes>
      </_SearchSerializationAssembly>
      <_TargetSerializationAssembly Include="@(_SearchSerializationAssembly)" Condition="$([System.String]::new('%(_SearchSerializationAssembly.Identity)').EndsWith('%(_SearchSerializationAssembly.AssemblyName).dll'))" />
      <_ReferenceSerializationAssemblyName Include="%(SerializationAssembly.Identity).XmlSerializers" />
      <_ReferenceSerializerIntermediateFolder Include="$(IntermediateOutputPath)%(_ReferenceSerializationAssemblyName.Identity)" />
    </ItemGroup>

    <Delete Files="%(_ReferenceSerializerIntermediateFolder.Identity).dll" ContinueOnError="true"/>
    <Delete Files="%(_ReferenceSerializerIntermediateFolder.Identity).cs" ContinueOnError="true"/>
    <Delete Files="%(_ReferenceSerializerIntermediateFolder.Identity).pdb" ContinueOnError="true"/>
    <Message Text="Running Serialization Tool for Reference Assembly" Importance="normal" />
    <Exec Command="dotnet Microsoft.XmlSerializer.Generator --force --quiet --reference &quot;@(ReferencePath)&quot; --assembly &quot;%(_TargetSerializationAssembly.Identity)&quot; --type %(_TargetSerializationAssembly.SerializationTypes) --out &quot;$(IntermediateOutputPath)&quot;" ContinueOnError="true" />
    <Warning Condition="Exists('$(IntermediateOutputPath)%(_ReferenceSerializationAssemblyName.Identity).cs') != 'true'" Text="SGEN: Fail to generate %(_ReferenceSerializationAssemblyName.Identity)'. Please follow the instructions at https://go.microsoft.com/fwlink/?linkid=858594 and try again." />
    <Csc Condition="Exists('$(IntermediateOutputPath)%(_ReferenceSerializationAssemblyName.Identity).cs') == 'true'" ContinueOnError="true" OutputAssembly="$(IntermediateOutputPath)%(_ReferenceSerializationAssemblyName.Identity).dll" References="@(ReferencePath);@(IntermediateAssembly)" Optimize="$(Optimize)" EmitDebugInformation="$(DebugSymbols)" Sources="$(IntermediateOutputPath)%(_ReferenceSerializationAssemblyName.Identity).cs" TargetType="Library" ToolExe="$(CscToolExe)" ToolPath="$(CscToolPath)" DisabledWarnings="$(_SerializationAssemblyDisabledWarnings)" NoStandardLib="true" NoConfig="true"/>
    <Warning Condition="Exists('$(IntermediateOutputPath)%(_ReferenceSerializationAssemblyName.Identity).dll') != 'true' And Exists('$(IntermediateOutputPath)%(_ReferenceSerializationAssemblyName.Identity).cs') == 'true'" Text="SGEN: Fail to compile %(_ReferenceSerializationAssemblyName.Identity).cs. Please follow the instructions at https://go.microsoft.com/fwlink/?linkid=858594 and try again." />
    <Copy Condition="Exists('%(_ReferenceSerializerIntermediateFolder.Identity).dll') == 'true'" SourceFiles="%(_ReferenceSerializerIntermediateFolder.Identity).dll" DestinationFolder="$(OutputPath)" />
  </Target>

  <Target Name="CleanReferenceSerializationAssemblies" AfterTargets="CoreClean" Condition="@(SerializationAssembly)!=''">
    <Message Text="Cleaning serialization files for reference assemblies ..." Importance="normal" />
    <Delete Condition="Exists('$(OutputPath)%(SerializationAssembly.Identity).XmlSerializers.dll') == 'true'" Files="$(OutputPath)%(SerializationAssembly.Identity).XmlSerializers.dll" />
  </Target>

  <Target Name="CopySerializerForReferenceAssemblies" AfterTargets="PrepareForPublish" Condition="@(SerializationAssembly)!=''">
    <Copy Condition="Exists('$(OutputPath)%(SerializationAssembly.Identity).XmlSerializers.dll') == 'true'" SourceFiles="$(OutputPath)%(SerializationAssembly.Identity).XmlSerializers.dll" DestinationFolder="$(PublishDir)" SkipUnchangedFiles="false" />
  </Target>

</Project>
